package org.jeecg.modules.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.service.CallbackService;
import org.jeecg.modules.temporary.entity.DiyTemporaryApplicantInfo;
import org.jeecg.modules.temporary.service.IDiyTemporaryApplicantInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 王鹏
 * @version 1.0
 * @description
 * @date 2020/7/30 3:01
 */
@Api(tags="实现临时申请人数据回显")
@RestController
@Slf4j
@RequestMapping("/temporary/diyTemporaryApplicantInfo")
public class DiyTemporaryController {
    @Autowired
    private IDiyTemporaryApplicantInfoService diyTemporaryApplicantInfoService;

    @Autowired
    private CallbackService callbackService;

    @PostMapping(value = "/SaveTemp")
    public Result<?> SaveTemp(@RequestBody DiyTemporaryApplicantInfo diyTemporaryApplicantInfo, HttpServletRequest request) {
        System.out.println("进入到保存controller");
        Subject subject = SecurityUtils.getSubject();
        LoginUser user = (LoginUser) subject.getPrincipal();
        log.info(user.toString());

        //获取token
        String token = request.getHeader("X-Access-Token");
        //获取当前登录用户的unionid username就是unionid
        String username = JwtUtil.getUsername(token);
        // 根据unionid获取userid
        String useridStr = callbackService.selUserIdByUnionid(username);
        JSONObject getUserId = JSON.parseObject(useridStr);
        String userid = getUserId.getString("userid");
        diyTemporaryApplicantInfo.setDingUserId(userid);
        //将表单数据存入数据库中
        diyTemporaryApplicantInfoService.inset(diyTemporaryApplicantInfo);
        return Result.ok("保存成功！");
    }

/**
 * 查询当前登录的临时申请人信息用来回显数据
 * */
    @GetMapping(value = "/selectByuserid")
    public Result<?> selectByuserid(HttpServletRequest request) {
        Subject subject = SecurityUtils.getSubject();
        LoginUser user = (LoginUser) subject.getPrincipal();

        //获取token
        String token = request.getHeader("X-Access-Token");
        //获取当前登录用户的unionid username就是unionid
        String username = JwtUtil.getUsername(token);
        // 根据unionid获取userid
        String useridStr = callbackService.selUserIdByUnionid(username);
        JSONObject getUserId = JSON.parseObject(useridStr);
        String userid = getUserId.getString("userid");
        DiyTemporaryApplicantInfo temp = diyTemporaryApplicantInfoService.selectByuserid(userid);
        Result result = new Result();
        if (temp != null) {
            result.setSuccess(true);

        } else {
            result.setSuccess(false);
        }
        return result.ok(temp);
    }
/*
* 判断当前临时申请人是否提交过，并且没有被拒绝，是否能进行临时申请人的转正审批提交
*
* */
    @GetMapping(value = "/selectIsdisabledByuserid")
    public Result<?> selectIsdisabledByuserid(HttpServletRequest request) {
        Subject subject = SecurityUtils.getSubject();
        LoginUser user = (LoginUser) subject.getPrincipal();

        //获取token
        String token = request.getHeader("X-Access-Token");
        //获取当前登录用户的unionid username就是unionid
        String username = JwtUtil.getUsername(token);
        // 根据unionid获取userid
        String useridStr = callbackService.selUserIdByUnionid(username);
        JSONObject getUserId = JSON.parseObject(useridStr);
        String userid = getUserId.getString("userid");

        int temp = diyTemporaryApplicantInfoService.selectIsDisabled(userid);
        Result result = new Result();

        return result.ok(temp);
    }
/**
 * 查询当前登录的临时申请人的信息
 */
    @GetMapping(value = "/seletidNull")
    public Result<?> seletidNull(HttpServletRequest request) {
        Subject subject = SecurityUtils.getSubject();
        LoginUser user = (LoginUser) subject.getPrincipal();

        //获取token
        String token = request.getHeader("X-Access-Token");
        //获取当前登录用户的unionid username就是unionid
        String username = JwtUtil.getUsername(token);
        // 根据unionid获取userid
        String useridStr = callbackService.selUserIdByUnionid(username);
        JSONObject getUserId = JSON.parseObject(useridStr);
        String userid = getUserId.getString("userid");

        DiyTemporaryApplicantInfo temp = diyTemporaryApplicantInfoService.seletidNull(userid);
        Result result = new Result();

        return result.ok(temp);
    }

    /**
     * 禁用当前登录的临时申请人的提交按钮的状态
     * @param request
     */
    @GetMapping(value = "/updateIsdisabledByuserid")
    public void updateIsdisabledByuserid(HttpServletRequest request) {
        System.out.println("进入修改临时申请人提交按钮禁用");
        Subject subject = SecurityUtils.getSubject();
        LoginUser user = (LoginUser) subject.getPrincipal();

        //获取token
        String token = request.getHeader("X-Access-Token");
        //获取当前登录用户的unionid username就是unionid
        String username = JwtUtil.getUsername(token);
        // 根据unionid获取userid
        String useridStr = callbackService.selUserIdByUnionid(username);
        JSONObject getUserId = JSON.parseObject(useridStr);
        String userid = getUserId.getString("userid");

        diyTemporaryApplicantInfoService.updateIsDisabled(userid);

    }
}
